<template>
  <div class="displist-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button :icon="Search" plain type="primary" @click="openQueryDialog">
          查询
        </el-button>
        <el-button :icon="Plus" plain type="primary" @click="openModuleDialog">
          新增检验标准
        </el-button>
        <el-button :icon="Plus" plain type="primary" @click="openItemDialog">
          新增检验明细
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table
      :cell-style="{ padding: '6px 6px' }"
      :data="standardDocList"
      highlight-current-row
      @cell-dblclick="showStandardDoc"
      @current-change="handleCurrentChange"
    >
      <el-table-column label="序号" type="index" width="80" />
      <el-table-column label="检验标准编码" prop="standardCode" width="300" />
      <el-table-column label="检验标准名称" prop="standardName" width="300" />
      <el-table-column label="备注" prop="remark" />
    </el-table>
    <el-pagination
      background
      :current-page="paginationParams.pageNum"
      :layout="layout"
      :page-size="paginationParams.pageSize"
      :total="paginationParams.total"
      @current-change="handlePaginationCurrentChange"
      @size-change="handlePaginationSizeChange"
    />
    <div style="height: 28px"></div>
    <el-table
      :cell-style="{ padding: '6px 6px' }"
      :data="checkItemList"
      highlight-current-row
      @cell-dblclick="showCheckItem"
    >
      <el-table-column label="序号" type="index" width="80" />
      <el-table-column label="编码" prop="itemCode" />
      <el-table-column label="名称" prop="itemName" />
      <el-table-column label="标准" prop="itemStandard" />
      <el-table-column label="备注" prop="remark" />
      <el-table-column label="不合格数量" prop="unquanlifiedQty" />
    </el-table>
    <queryDialog ref="queryDialogRef" @success-callback="queryCallback" />
    <StandatdModuleVue
      ref="standatdModuleRef"
      @success-callback="queryModuleCallback"
    />
    <CheckItemVue ref="chekcItemRef" @success-callback="queryItemCallback" />
  </div>
</template>

<script setup>
  import { Search, Plus } from '@element-plus/icons-vue'
  import { getStandardPageApi, getCheckItemApi } from '@/api/qm/standard_api'
  import { localStorageConfig } from '@/config/localstorage.config'
  import queryDialog from './modules/queryDialog.vue'
  import StandatdModuleVue from './modules/standardModule.vue'
  import CheckItemVue from './modules/checkItem.vue'

  const $baseLoading = inject('$baseLoading')
  const $baseMessage = inject('$baseMessage')

  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)

  const queryForm = ref({ pkCorp: pkCorp })

  const standardDocList = ref()
  const standardDoc = ref()
  const checkItemList = ref()
  const queryDialogRef = ref()
  const standatdModuleRef = ref()
  const chekcItemRef = ref()

  const paginationParams = reactive({
    pageNum: 1,
    pageSize: 10,
    total: 0,
  })

  const queryStandardDoc = async () => {
    const load = $baseLoading()
    try {
      let params = {
        ...queryForm.value,
        ...paginationParams,
      }
      const { code, msg, data } = await getStandardPageApi(params)
      if (code === 2000) {
        $baseMessage(msg, 'success')
        standardDocList.value = data.list
        paginationParams.total = data.total
      } else {
        $baseMessage(msg, 'error')
      }
    } finally {
      load.close()
    }
  }

  const openQueryDialog = () => queryDialogRef.value.openDialog()
  const openModuleDialog = () => {
    standatdModuleRef.value.openModuleDialog()
  }
  const openItemDialog = () => {
    if (standardDoc.value && standardDoc.value.pkStandardDoc) {
      chekcItemRef.value.openItemDialog(standardDoc.value)
    } else {
      $baseMessage('请选择需要添加明细的标准!', 'warning')
    }
  }

  const queryCallback = (value) => {
    queryForm.value = value
    queryStandardDoc()
  }
  const queryModuleCallback = () => {
    queryStandardDoc()
  }
  const queryItemCallback = () => {
    handleCurrentChange(standardDoc.value)
  }

  const handleCurrentChange = async (row) => {
    const load = $baseLoading()
    standardDoc.value = row
    try {
      let params = {
        pkStandardDoc: row.pkStandardDoc,
      }
      const { code, data } = await getCheckItemApi(params)
      if (code == 2000) {
        checkItemList.value = data
      }
    } finally {
      load.close()
    }
  }
  const showStandardDoc = (row) => {
    standatdModuleRef.value.openModuleDialog(row)
  }
  const showCheckItem = (row) => {
    chekcItemRef.value.openItemDialog(row)
  }

  //#region pagintion 开始
  const layout = ref('prev, pager, next')
  const handlePaginationCurrentChange = (...args) => {
    paginationParams.pageNum = args[0] ? args[0] : 1
  }
  const handlePaginationSizeChange = (...args) => {
    paginationParams.pageSize = args[0] ? args[0] : 20
    paginationParams.pageNum = 1
  }
  //#endregion

  watch(paginationParams, () => queryStandardDoc())

  onMounted(() => queryStandardDoc())
</script>
